﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InTheHand.Net.Sockets;
using WinHBM.Core.Bluetooth;

namespace WinHBM.Test.UI
{

    enum BluetoothCmd
    {
        ReadTeacherId,
        GetCacheCount,
        GetCacheFlower,
        ReadFlowerId
    }

    



    public partial class TestBluetooth : Form
    {

        class ComBoxItem
        {
            

            public ComBoxItem(BluetoothCmd command,string name)
            {
                this.command = command;
                this.name = name;
            }

            public BluetoothCmd command { get; set; }
            public string name { get; set; }


            public override string ToString()
            {
                return this.name;
            }
        }

        public TestBluetooth()
        {
            InitializeComponent();
        }


        private MagicBar bar;
        BluetoothClient client;

        private void btbClear_Click(object sender, EventArgs e)
        {
            this.txtResult.Clear();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            BluetoothManager manager = new BluetoothManager();
            client = manager.Connect("00:15:83:30:6E:24");
            bar = new MagicBar(client);
            this.result("[00:15:83:30:6E:24] 连接成功");
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            BluetoothManager manager = new BluetoothManager();
            manager.FoundDevice += Manager_FoundDevice;
            manager.DiscoveryCompleted += Manager_DiscoveryCompleted;
            this.result("开始扫描");
            manager.StartDiscovery();
        }

        private void Manager_DiscoveryCompleted()
        {
            this.result("扫描完毕");
        }

        private void Manager_FoundDevice(BluetoothManager manager, BluetoothDeviceInfo device)
        {
            this.result("找到设备："+device.DeviceAddress);
        }

        private void result(string str)
        {
            this.txtResult.AppendText(str+"\n");
        }

        private void TestBluetooth_Load(object sender, EventArgs e)
        {
            
            this.cmbCmd.Items.Add(new ComBoxItem(BluetoothCmd.GetCacheFlower, "获取红花缓存数据"));
            this.cmbCmd.Items.Add(new ComBoxItem(BluetoothCmd.ReadTeacherId, "获取教师ID"));
            this.cmbCmd.Items.Add(new ComBoxItem(BluetoothCmd.ReadFlowerId, "获取红花ID"));
            this.cmbCmd.Items.Add(new ComBoxItem(BluetoothCmd.GetCacheCount, "获取缓存数量"));
            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            ComBoxItem item = (ComBoxItem)this.cmbCmd.SelectedItem;
            switch(item.command)
            {
                case BluetoothCmd.GetCacheFlower:
                    RedFlower flower = this.bar.CMD_GetRedData();
                    this.result(flower.ToString());
                    break;
                case BluetoothCmd.ReadFlowerId:
                    this.result(this.bar.CMD_ReadRedCard().ToString());
                    break;
                case BluetoothCmd.ReadTeacherId:
                    this.result(this.bar.CMD_ReadTeacherId().ToString());
                    break;
                case BluetoothCmd.GetCacheCount:
                    this.result(this.bar.CMD_GetCacheCount().ToString());
                    break;
            }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            client.Close();
            bar = null;
            client = null;
            this.result("已断开连接");
        }
    }
}
